#include<stdlib.h>
//۵ڶ
struct ListNode 
{
      int val;
      struct ListNode *next;
};

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode* cur1 = l1, * cur2 = l2;
    struct ListNode* l3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    l3->next = NULL;
    l3->val = 0;
    struct ListNode* cur3 = l3, * prev = NULL;
    int nextnum = 0;
    while (cur1 && cur2)
    {
        cur3->val = (cur1->val + cur2->val + nextnum) % 10;
        if (cur1->val + cur2->val + nextnum >= 10)
        {
            nextnum = 1;
        }
        else
        {
            nextnum = 0;
        }
        cur1 = cur1->next;
        cur2 = cur2->next;
        cur3->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        prev = cur3;
        cur3->next->next = NULL;
        cur3 = cur3->next;
    }
    while (cur1)
    {
        cur3->val = (cur1->val + nextnum) % 10;
        if (cur1->val + nextnum >= 10)
        {
            nextnum = 1;
        }
        else
        {
            nextnum = 0;
        }
        cur1 = cur1->next;

        cur3->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        prev = cur3;
        cur3 = cur3->next;

    }
    while (cur2)
    {
        cur3->val = (cur2->val + nextnum) % 10;
        if (cur2->val + nextnum >= 10)
        {
            nextnum = 1;
        }
        else
        {
            nextnum = 0;
        }
        prev = cur3;

        cur2 = cur2->next;
        cur3->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        cur3 = cur3->next;
    }
    if (nextnum == 1)
    {
        cur3->val = 1;
        cur3->next = NULL;
    }
    if (nextnum == 0)
    {
        free(cur3);
        cur3 = NULL;
        prev->next = NULL;
    }



    return l3;

}

int main()
{

}